<?php
namespace app\admin\controller;

class Industry extends Common
{
    public function lists()
    {
        $name = input('name');
        $parent_id = input('parent_id');
        $cid = input('cid');
        
        if(empty($cid)){
            $cid = $parent_id;
        }

        $where = [];
        if(!empty($name)){
            $where['name'] = $name;
        }

        if(!empty($cid)){
            $where['cid'] = $cid;
        }

        $industry_category_nav = [];

        $industry_category = db('industry_category')->where(['parent_id'=>'0'])->select();

        if(!empty($parent_id)){
            $industry_category_nav = db('industry_category')->where(['parent_id'=>$parent_id])->select();
        }
        

        $industry = db('industry')->where($where)->paginate(10)->each(function($item){
            $name = db('industry_category')->where(['id'=>$item['cid']])->value('name');

            if(!empty($name)){
                $item['cateogry_name'] = $name;
            }else{
                $item['cateogry_name'] = '';
            }   

            return $item;
        });

        $industry_category = db('industry_category')->where(['parent_id'=>'0'])->select();

        $this->assign('industry_category',$industry_category);
        $this->assign('parent_id',$parent_id);
        $this->assign('cid',$cid);
        $this->assign('industry',$industry);
        $this->assign('industry_category',$industry_category);
        $this->assign('industry_category_nav',$industry_category_nav);
        return $this->fetch();
    }

    public function lists_del(){
        $id = input('id');

        db('industry')->where(['id'=>$id])->delete();

        $this->redirect("Industry/lists");

        return $this->fetch();
    }

    public function add_lists(){
        $id = input('id');

        $industry = db('industry')->where(['id'=>$id])->find();

        $industry_category = db('industry_category')->where(['parent_id'=>'0'])->select();

        $industry_category_nav = [];

        if(!empty($industry)){
            $parent_category_cid = db('industry_category')->where(['id'=>$industry['cid']])->find();
            if(!empty($parent_category_cid['parent_id'])){
                $parent_category_cid['parent_id'] = $parent_category_cid['parent_id'];
                $industry_category_nav = db('industry_category')->where(['parent_id'=>$parent_category_cid['parent_id']])->select();
            }else{
                $parent_category_cid['parent_id'] = $industry['cid'];
            }
        }else{
            $parent_category_cid['parent_id'] = '0';
        }

        if(request()->isPost()){
            $parent_id = input('parent_id');
            $name = input('name');
            if(empty($parent_id))$this->error('请选择父分类');
            $cid = input('cid');

            if(empty($cid)){
                $cid = $parent_id;
            }

            $data['cid'] = $cid;
            $data['name'] = $name;

            if(!empty($id)){
                db('industry')->where(['id'=>$id])->update($data);
            }else{
                db('industry')->insert($data);
            }

            $this->redirect("Industry/lists");
        }

        $this->assign('industry',$industry);
        $this->assign('parent_category_cid',$parent_category_cid);
        $this->assign('industry_category_nav',$industry_category_nav);
        $this->assign('industry_category',$industry_category);
        return $this->fetch();
    }

    //查询子分类
    public function select_catgory_result(){
        $id = input('parent_id');

        $result = db('industry_category')->where(['parent_id'=>$id])->select();

        $this->success($result);
    }

    public function cateogry()
    {
        $name = input('name');
        

        $where = [];
        if(!empty($name)){
            $where['name'] = $name;
        }
        $industry_category = db('industry_category')->where($where)->paginate(10)->each(function($item){
            if(!empty($item['parent_id'])){
                $industry_category_name = db('industry_category')->where(['id'=>$item['parent_id']])->value('name');
                $item['parent_name'] = $industry_category_name;
            }else{
                $item['parent_name'] = '';
            }

            return $item;
        });
        $page = $industry_category->render();
        $this->assign('industry_category',$industry_category);
        $this->assign('page',$page);
        return $this->fetch();
    }

    public function add_cateogry()
    {
        $id = input('id');
        $industry_category = db('industry_category')->where(['parent_id'=>'0'])->select();
        $industry_category_result = db('industry_category')->where(['id'=>$id])->find();

        if(request()->isPost()){
            $parent_id = input('parent_id');
            $name = input('name');
            
            if(empty($name))$this->error('请填写分类名称!');
            
            $data['name'] = $name;

            if(!empty($id)){
                db('industry_category')->where(['id'=>$id])->update($data);
            }else{
                $data['parent_id'] = $parent_id;
                db('industry_category')->insert($data);
            }

            $this->redirect("Industry/cateogry");
        }

        $this->assign('industry_category',$industry_category);
        $this->assign('industry_category_result',$industry_category_result);
        return $this->fetch();
    }
}
